home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: kakima@ix.netcom.com (Kiyoshi Akima)
- Newsgroups: comp.lang.c++
- Subject: GCC/STL problem
- Date: 7 Feb 1996 05:05:36 GMT
- Organization: Netcom
- Message-ID: <4f9bv0$iec@cloner3.netcom.com>
- NNTP-Posting-Host: ix-den12-15.ix.netcom.com
- X-NETCOM-Date: Tue Feb 06 9:05:40 PM PST 1996
-
- I'm having a problem with a destructor getting called twice.
-
- //begin code
- #include <iostream.h>
- #include <list.h>
-
- class Foo {
- public:
- Foo(const int i = 0): i_(i) {}
- Foo(const Foo& f): i_(f.i_) {}
- Foo& operator =(const Foo& f) { i_ = f.i_; }
- ~Foo() {
- cout << "dtor(" << i_ << "): " << (void*)this << '\n';
- i_ = -1;
- }
- private:
- int i_;
- };
-
- int main()
- {
- list<Foo> f;
- f.push_back(Foo(1));
- return 0;
- }
- // end code
-
- The code doesn't do much. When the destructor gets called, it displays
- its address and sets the integer member i_ to -1.
- When I compile and run with g++ 2.7.2 on Linux using the bundled STL I
- get the following output:
-
- dtor(1): 0xbffffbc8
- dtor(1): 0x8009b78
- dtor(-1): 0x8009b78
- dtor(0): 0x8009dd8
-
- I understand the temporary object getting constructed and destructed.
- I also understand the "empty" object getting placed in the list.
- But the third line of output tells me that the element in the list is
- getting destructed TWICE.
-
- Kiyoshi Akima
- kakima@ix.netcom.com
-
-